feat: cdp keep alive and exit strategy#365
Conversation
Greptile SummaryThis PR adds two resilience features to the CDP connection layer: a keepalive mechanism that detects zombie WebSocket connections (by periodically sending Key changes:
Issues found:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant HC as Health Poller
participant HR as HealthRoute (watchdog)
participant B as Browser
participant CDP as CdpBackend
participant CR as Chromium
Note over HR: watchdog starts on creation (5 min timer)
loop every N seconds
HC->>HR: GET /health
HR->>HR: resetWatchdog()
HR->>B: isCdpConnected()
B->>CDP: isConnected()
CDP-->>B: bool
B-->>HR: bool
HR-->>HC: {status:"ok", cdpConnected:bool}
end
Note over CDP: keepalive loop every 30s
CDP->>CR: Browser.getVersion (10s race timeout)
alt success
CR-->>CDP: version response
CDP->>CDP: clearTimeout(pendingTimer)
else timeout / send failure
CDP->>CDP: handleDeadConnection()
CDP->>CDP: stopKeepalive()
CDP->>CDP: ws.close() → ws=null
CDP->>CDP: handleUnexpectedClose()
CDP->>CDP: rejectPendingRequests()
CDP->>CDP: reconnectWithRetries() [up to 3×, 5s delay each]
alt reconnect succeeds
CDP->>CR: connect WebSocket
CR-->>CDP: open
CDP->>CDP: startKeepalive()
else all retries exhausted
CDP->>CDP: process.exit(GENERAL_ERROR)
end
end
alt no health check for 5 min
HR->>HR: watchdog fires
HR->>HR: process.exit(GENERAL_ERROR)
end
Last reviewed commit: 193151d |
Additional Comments (1)
capture the socket reference in the Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/server/src/browser/backends/cdp.ts
Line: 81
Comment:
resetting `disconnecting` to false here creates a race condition - if an old socket's `onclose` handler fires after a new connection establishes, it will set `this.connected = false` and `this.ws = null`, breaking the new connection
capture the socket reference in the `onclose` closure and check `if (this.ws !== currentWs) return` before modifying shared state
How can I resolve this? If you propose a fix, please make it concise. |
No description provided.